package com.ph.util;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.ph.constant.LoginConstant;
import com.ph.entity.CallBackContent;
import com.ph.entity.LoginCallBackData;

public class BackParamHandleUtil {

	/**
	 * 
	 * @Title: jumpPackBack   
	 * @Description: 封装跳转路径   
	 * @param: @param param
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	public static String jumpPackBack(LoginCallBackData param) {
		if (param != null) {
			String type = param.getJumpType();
			String url = param.getJumpUrl();
			if (StringUtils.isNotBlank(url)) {
				List<CallBackContent> data = param.getData();
				if (LoginConstant.REQUEST_GET.equalsIgnoreCase(type)) {
					return getUrlByGet(data, url);
				} else if (LoginConstant.REQUEST_POST.equalsIgnoreCase(type)) {
					return getUrlByPost(data, url);
				}
			}
		}
		return null;
	}
	/**
	 * 
	 * @Title: getUrlByGet   
	 * @Description: 返回 get 路径请求   
	 * @param: @param data
	 * @param: @param url
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	private static String getUrlByGet(List<CallBackContent> data,String url){
		StringBuilder sbud = new StringBuilder();
		sbud.append(url);
		if (data != null && !data.isEmpty()) {
			sbud.append("?");
			int num = 0;
			for (CallBackContent entry : data) {
				if (num == 0) {
					sbud.append(entry.getKey()).append("=").append(entry.getValue());
				} else {
					sbud.append("&").append(entry.getKey()).append("=").append(entry.getValue());
				}
				num++;
			}
		}
		return sbud.toString();
	}
	/**
	 * 
	 * @Title: getUrlByPost   
	 * @Description: 返回post 路径   
	 * @param: @param data
	 * @param: @param url
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	private static String getUrlByPost(List<CallBackContent> data, String url) {
		StringBuilder sbud = new StringBuilder();
		if (data != null && !data.isEmpty()) {
			sbud.append("<form action=").append("\"").append(url).append("\" ")
					.append(" method=\"post\" style=\"display: none;\" id = \"callback_id\">");
			for (CallBackContent entry : data) {
				sbud.append("<input name=").append("\"").append(entry.getKey()).append("\"").append(" value=")
						.append("\"").append(entry.getValue()).append("\" ").append(" />");
			}
			sbud.append("</form>");
		}
		return sbud.toString();
	}
}
